/*
 * Copyright (c) 2013 Eugene Kuzmenko
 *
 * Permission  is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in  the Software without restriction, including without limitation the rights
 * to  use,  copy,  modify,  merge, publish, distribute, sublicense, and/or sell
 * copies  of  the  Software,  and  to  permit  persons  to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The  above  copyright  notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE  IS  PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED,  INCLUDING  BUT  NOT  LIMITED  TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS  FOR  A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS  OR  COPYRIGHT  HOLDERS  BE  LIABLE  FOR  ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * For more information, see <http://www.aljey.com>.
 */

//=al://Al\Style:VCL/Window/Confirm
//=MappingWindow
//=CrudWindow

$(function () {
  var db_panel_connection = $('#db_panel_connection').contents()[0],
    db_panel_database = $('#db_panel_database'),
    db_panel_database_text = db_panel_database.contents()[0];
  Class.create('Button', {
    renderTo: '#db_panel_header',
    style: 6,
    text: 'connection settings',
    css: {padding: '0 12px 0 24px', marginTop: '-2px'},
    handler: function () { Al.alert('Connection settings', window.db_config) }
  }).show();
  Class.create('Button', {
    renderTo: '#db_panel_header',
    style: 6,
    text: 'mapping settings',
    css: {padding: '0 12px', marginTop: '-2px'},
    handler: function () { Al.alert('Mapping settings', window.mapping) }
  }).show();
  function sendRequest(path, fn) {
    Al.confirm('Confirm', 'Are you sure?', function () {
      Al.LoadMask.show();
      $.post('/admin/' + path,
        function (r) {
          fn && fn(r);
          Al.LoadMask.hide();
          Al.alert('Info', r.message);
        });
    });
  }
  Class.create('Button', {
    renderTo: db_panel_database,
    style: 6,
    css: {padding: '0 12px', marginTop: '-2px'},
    text: window.db_to_create ? 'Create' : 'Remove',
    handler: function () {
      var me = this;
      sendRequest((window.db_to_create ? 'create' : 'remove') + '-db',
        function (r) {
          db_panel_connection.nodeValue = r.connected;
          db_panel_database_text.nodeValue = r.db;
          window.db_config = r.db_config;
          window.mapping = r.mapping;
          me.setText((window.db_to_create = r.db_to_create) ? 'Create' :
            'Remove');
        });
    }
  }).show();
  Class.create('Button', {
    renderTo: '#db_panel_schema_create',
    style: 6,
    css: {padding: '0 12px 0 0', marginTop: '-2px'},
    text: 'Create schema',
    handler: function () { sendRequest('create-schema') }
  }).show();
  Class.create('Button', {
    renderTo: '#db_panel_schema_update',
    style: 6,
    css: {padding: '0 12px 0 0', marginTop: '-2px'},
    text: 'Update schema',
    handler: function () { sendRequest('update-schema') }
  }).show();
  Class.create('Button', {
    renderTo: '#db_panel_schema_remove',
    style: 6,
    css: {padding: '0 12px 0 0', marginTop: '-2px'},
    text: 'Remove schema',
    handler: function () { sendRequest('remove-schema') }
  }).show();
  Class.create('Button', {
    renderTo: '#db_panel_generate_entities',
    style: 6,
    css: {padding: '0 12px 0 0', marginTop: '-2px'},
    text: 'Generate entities',
    handler: function () { sendRequest('generate-entities') }
  }).show();
  Class.create('Button', {
    renderTo: '#db_panel_clear_cache',
    style: 6,
    css: {padding: '0 12px 0 0', marginTop: '-2px'},
    text: 'Clear cache',
    handler: function () { sendRequest('clear-db-cache') }
  }).show();
  Class.create('Button', {
    renderTo: '#db_panel_load_fixtures',
    style: 6,
    css: {padding: '0 12px 0 0', marginTop: '-2px'},
    text: 'Load data fixtures',
    handler: function () { sendRequest('load-fixtures') }
  }).show();
  Class.create('Button', {
    renderTo: '#db_panel_import_mapping',
    style: 6,
    css: {padding: '0 12px 0 0', marginTop: '-2px'},
    text: 'Import mapping',
    handler: function () {
      Class.create('MappingWindow').show();
    }
  }).show();
  Class.create('Button', {
    renderTo: '#db_panel_generate_crud',
    style: 6,
    css: {padding: '0 12px 0 0', marginTop: '-2px'},
    text: 'Generate CRUD',
    handler: function () {
      Class.create('CrudWindow').show();
    }
  }).show();
});